package cn.szzsi.platform.controller.admin;

import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.common.util.RegexUtils;
import cn.szzsi.model.*;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.codec.digest.DigestUtils;
import org.eclipse.jetty.util.security.Credential;

import java.util.Date;
import java.util.List;

/**
 * 超管用户管理
 * Created by admin on 2017/7/7.
 */
public class UserController extends Controller{
    private Admin admin;
    public void list(){
        render(CommonUtil.TEMPLATE_PATH + "/platform/usermanage/user_manage_list.html");
    }
    public void ajaxlist(){
        List<Admin> la = Admin.dao.find("select a.*,p.name roleName from sys_admin a left join sys_platform_role p on a.roleId=p.id where a.username!='admin' order by a.createDate desc");
        renderJson(la);
    }
    public void add(){
        setAttr("roleList", PlatformRole.dao.getAllRole());
        render(CommonUtil.TEMPLATE_PATH + "/platform/usermanage/user_manage_input.html");
    }
    public void save(){
        admin=getModel(Admin.class);
        String rePassword=getPara("rePassword");
        if(Admin.dao.isExistAdmin(admin.getUsername())){
            renderJson(Ret.fail("info","登录名已存在"));
            return;
        }
        if(!RegexUtils.checkNumLN(admin.getUsername())){
            renderJson(Ret.fail("info","登录名只能输入数字、字母或者下划线"));
            return;
        }
        if(!admin.getPassword().equals(rePassword)){
            renderJson(Ret.fail("info","两次密码输入不一致"));
            return;
        }

        /*if(StrKit.notBlank(admin.getPhone())&&!RegexUtils.checkMobile(admin.getPhone())){
            renderJson(Ret.fail("info","请输入正确的手机号"));
            return;
        }*/
        if(StrKit.notBlank(admin.getPhone())&&!RegexUtils.checkEmail(admin.getEmail())){
            renderJson(Ret.fail("info","请输入正确的邮箱"));
            return;
        }
        admin.setId(StrKit.getRandomUUID());

        admin.setCreateDate(new Date());
        admin.save();
        renderJson(Ret.ok("info","添加成功").set("url","list"));

    }
    public void edit(){
        Admin admin=Admin.dao.findById(getPara("id"));
        setAttr("admin",admin);
        setAttr("roleList", PlatformRole.dao.getAllRole());
        render(CommonUtil.TEMPLATE_PATH + "/platform/usermanage/user_manage_input.html");
    }
    public void update(){
        admin=getModel(Admin.class);
        String rePassword=getPara("rePassword");
        if(!RegexUtils.checkNumLN(admin.getUsername())){
            renderJson(Ret.fail("info","登录名只能输入数字、字母或者下划线"));
            return;
        }
        if(StrKit.notBlank(admin.getPassword())){
            if(!admin.getPassword().equals(rePassword)){
                renderJson(Ret.fail("info","两次密码输入不一致"));
                return;
            }
        }


        /*if(StrKit.notBlank(admin.getPhone())&&!RegexUtils.checkMobile(admin.getPhone())){
            renderJson(Ret.fail("info","请输入正确的手机号"));
            return;
        }*/
        if(StrKit.notBlank(admin.getPhone())&&!RegexUtils.checkEmail(admin.getEmail())){
            renderJson(Ret.fail("info","请输入正确的邮箱"));
            return;
        }
        if(StrKit.isBlank(admin.getPassword())){
            admin.setPassword(Admin.dao.findById(admin.getId()).getPassword());
        }else{
            admin.setPassword(DigestUtils.md5Hex(admin.getPassword()));
        }


        admin.setModifyDate(new Date());
        admin.update();
        renderJson(Ret.ok("info","修改成功").set("url","list"));
    }


    public void delete(){
        String id=getPara("id");
        Admin.dao.deleteById(id);
        renderJson(Ret.ok("info","操作成功"));
    }

    public static void main(String[] args) {
        System.out.println(StrKit.getRandomUUID());
    }
}
